<template>
  <div>
    <base-list ref="accountGroupView"
               :condition="condition"
               :datas.sync="accountGroupData"
               :pageSearch="pageSearch">
      <el-form :inline="true"
               :model="condition"
               class="search-form"
               slot="search"
               label-width="100px">
        <el-row>
          <el-col :span="7" class="search-width6">
            <el-form-item label="账户组名称">
              <auto-search-account-group @selectName="v=>condition['search_LIKE_name']=v"></auto-search-account-group>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button type="primary"
                       icon="el-icon-search"
                        class="ml-80"
                       @click="loadData">查 询</el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-row>
              <el-table ref="accountGroup"
                        :data="accountGroupData"
                        border
                        highlight-current-row
                        @current-change="handleCurrentChange">
                <el-table-column label="序号"
                                 type="index"
                                 width="50"></el-table-column>
                <el-table-column prop="name"
                                 label="账户组名称"
                                 min-width="100"></el-table-column>
                <el-table-column align="center"
                                 label="操作"
                                 width="100">
                  <template slot-scope="scope">
                    <el-button size="small"
                               type="text"
                               @click.stop="handleDialogFormOpen(true,scope.row)">编辑</el-button>
                    <el-button type="text"
                               class="danger"
                               size="small"
                               @click="handleRemove(scope.row)">删除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-row>
          </el-col>
          <el-col :span="18">
            <el-row>
              <el-table ref="account"
                        :data="accountData"
                        border>
                <el-table-column label="序号"
                                 type="index"
                                 width="50"></el-table-column>
                <el-table-column prop="mnemonicCode"
                                 label="助记码"
                                 min-width="120"></el-table-column>
                <el-table-column prop="name"
                                 label="账户名称"
                                 min-width="120"></el-table-column>
                <el-table-column prop="orgName"
                                 label="客户名称"
                                 min-width="120"></el-table-column>
              </el-table>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
    </base-list>
    <div style="text-align:center">
      <el-button plain
                 type="info"
                 @click="handleRouterPush({path:'/goods/business',name:'商务管理工作台'})">返回</el-button>
    </div>
    <add-account-group-form ref="addAccountGroupForm"
                            @load="loadData"></add-account-group-form>
  </div>
</template>

<script>
import {
  getAccountGroupList,
  delAccountGroup
} from '@/api/commerce/accountGroup' //左右表格来自偶同一个接口
import autoSearchAccountGroup from '@/components/autocomplete/autoSearchAccountGroup'
import addAccountGroupForm from './addAccountGroupForm'

export default {
  name: 'accountGroupView',
  components: {
    autoSearchAccountGroup,
    addAccountGroupForm
  },
  data() {
    return {
      form: this.clearForm(),
      condition: {}, // 查询条件
      accountGroupData: [], // 左边表格
      accountData: [], //右边表格
      pageSearch: getAccountGroupList, // 分页查询api接口
      currentRow: null
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    clearForm() {
      return {
        orgId: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.name
      }
    },
    loadData() {
      this.$refs.accountGroupView.loadData()
    },
    // 编辑
    handleDialogFormOpen(bool, item) {
      console.log(item)
      if (bool) {
        this.$refs.addAccountGroupForm.form = item
        this.$refs.addAccountGroupForm.isEdit = true
        this.$refs.addAccountGroupForm.dialogForm = true
      } else {
        this.$refs.addAccountGroupForm.dialogForm = true
      }
    },
    handleRemove(item) {
      this.$confirm(`是否删除该数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delAccountGroup(item.id).then(res => {
            this.$message({
              type: 'success',
              message: `删除成功!`
            })
            this.loadData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    handleCurrentChange(row) {
      this.currentRow = row
      this.accountData = row.cargoOwnerAccounts
    },
    handleRouterPush(val) {
      this.$store.commit('ADD_CACHED_VIEWS', val)
      this.$router.push(val.path)
    }
  }
}
</script>

<style scoped>
</style>
